package com.nutiteq.directions;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import com.nutiteq.components.WgsPoint;
import com.nutiteq.services.YourNavigationDirections;

public class YourNavigationOptionsForm extends Form implements CommandListener {
  private final DirectionsCanvas directionsCanvas;
  private final WgsPoint start;
  private final WgsPoint end;

  private final Command ok;
  private final Command back;
  private final ChoiceGroup vehicle;
  private final ChoiceGroup type;

  public YourNavigationOptionsForm(final DirectionsCanvas directionsCanvas, final WgsPoint start,
      final WgsPoint end) {
    super("YourNavigation routing");
    this.directionsCanvas = directionsCanvas;
    this.start = start;
    this.end = end;

    vehicle = new ChoiceGroup("Transport", ChoiceGroup.POPUP);
    vehicle.append("Car", null);
    vehicle.append("Bicycle", null);
    vehicle.append("Foot", null);
    type = new ChoiceGroup("Type", ChoiceGroup.POPUP);
    type.append("Fastest", null);
    type.append("Shortest", null);

    append(vehicle);
    append(type);

    ok = new Command("Ok", Command.OK, 0);
    back = new Command("Back", Command.BACK, 0);

    addCommand(ok);
    addCommand(back);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      directionsCanvas.show(directionsCanvas);
    } else if (cmd == ok) {
      final String v = getVehicle(vehicle.getSelectedIndex());
      final int t = type.getSelectedIndex() == 0 ? YourNavigationDirections.ROUTE_TYPE_FASTEST
          : YourNavigationDirections.ROUTE_TYPE_SHORTEST;
      directionsCanvas.setDirectionsService(new YourNavigationDirections(directionsCanvas, start,
          end, v, t));
    }
  }

  private String getVehicle(final int selectedIndex) {
    if (selectedIndex == 0) {
      return YourNavigationDirections.MOVE_METHOD_CAR;
    } else if (selectedIndex == 1) {
      return YourNavigationDirections.MOVE_METHOD_BICYCLE;
    } else {
      return YourNavigationDirections.MOVE_METHOD_FOOT;
    }
  }
}
